package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.EmgDisasterBo;
import com.ruoyi.system.domain.vo.EmgDisasterVo;
import com.ruoyi.system.domain.EmgDisaster;
import com.ruoyi.system.mapper.EmgDisasterMapper;
import com.ruoyi.system.service.IEmgDisasterService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 灾情Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-16
 */
@RequiredArgsConstructor
@Service
public class EmgDisasterServiceImpl implements IEmgDisasterService {

    private final EmgDisasterMapper baseMapper;

    /**
     * 查询灾情
     */
    @Override
    public EmgDisasterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询灾情列表
     */
    @Override
    public TableDataInfo<EmgDisasterVo> queryPageList(EmgDisasterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmgDisaster> lqw = buildQueryWrapper(bo);
        Page<EmgDisasterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询灾情列表
     */
    @Override
    public List<EmgDisasterVo> queryList(EmgDisasterBo bo) {
        LambdaQueryWrapper<EmgDisaster> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmgDisaster> buildQueryWrapper(EmgDisasterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmgDisaster> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EmgDisaster::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getDisasterTypeId()), EmgDisaster::getDisasterTypeId, bo.getDisasterTypeId());
        lqw.eq(bo.getDisasterTime() != null, EmgDisaster::getDisasterTime, bo.getDisasterTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), EmgDisaster::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getReportPerson()), EmgDisaster::getReportPerson, bo.getReportPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getReportPersonPhone()), EmgDisaster::getReportPersonPhone, bo.getReportPersonPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), EmgDisaster::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), EmgDisaster::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), EmgDisaster::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getImage1()), EmgDisaster::getImage1, bo.getImage1());
        lqw.eq(StringUtils.isNotBlank(bo.getImage2()), EmgDisaster::getImage2, bo.getImage2());
        lqw.eq(StringUtils.isNotBlank(bo.getImage3()), EmgDisaster::getImage3, bo.getImage3());
        lqw.eq(StringUtils.isNotBlank(bo.getImage4()), EmgDisaster::getImage4, bo.getImage4());
        lqw.eq(StringUtils.isNotBlank(bo.getImage5()), EmgDisaster::getImage5, bo.getImage5());
        lqw.eq(StringUtils.isNotBlank(bo.getImage6()), EmgDisaster::getImage6, bo.getImage6());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessDescription()), EmgDisaster::getProcessDescription, bo.getProcessDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EmgDisaster::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增灾情
     */
    @Override
    public Boolean insertByBo(EmgDisasterBo bo) {
        EmgDisaster add = BeanUtil.toBean(bo, EmgDisaster.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改灾情
     */
    @Override
    public Boolean updateByBo(EmgDisasterBo bo) {
        EmgDisaster update = BeanUtil.toBean(bo, EmgDisaster.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmgDisaster entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除灾情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
